package com.mobile.collect.net;

import com.mobile.collect.activitys.CollectApplication;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NLog;
import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;

public abstract class MyApiSubcriber<K, T extends BaseObjBean> extends ApiSubcriber<T> {
    XPresent xPresent;
    boolean controlDialog = true;

    public MyApiSubcriber(XPresent xPresent) {
        this.xPresent = xPresent;
    }

    public MyApiSubcriber(XPresent xPresent, boolean controlDialog) {
        this.xPresent = xPresent;
        this.controlDialog = controlDialog;
    }

    @Override
    protected void onFail(NetError error) {
        NLog.e("MyApiSubcriber", "MyApiSubcriberMyApiSubcriberMyApiSubcriberMyApiSubcriber");

        NLog.e("MyApiSubcriber", "error = " + error.getMessage());
        xPresent.getV().disLoadDialog();
        xPresent.getV().twinkCompleted();
        switch (error.getType()) {
            case NetError.NoConnectError:
                if (Kits.NetWork.getNetworkTypeName(CollectApplication.getContext()) == Kits.NetWork.NETWORK_TYPE_DISCONNECT) {
                    if (!xPresent.getV().showNoNetPage()) {
                        xPresent.getV().showToast(error.getMessage());
                    }
                } else {
                    xPresent.getV().showToast(error.getMessage());
                }
                break;
            case NetError.ParseError:
                boolean isShow = xPresent.getV().showNoDataPage();
                if (!isShow) {
                    xPresent.getV().showToast(error.getMessage());
                } else {
                    xPresent.getV().showNoDataPage();
                }
                break;
            default:
                xPresent.getV().showToast(error.getMessage());
        }
    }

    @Override
    public void onComplete() {
        super.onComplete();
        xPresent.getV().twinkCompleted();
        xPresent.getV().disLoadDialog();
    }


    @Override
    public void onNext(T t) {
        if (controlDialog) {
            xPresent.getV().disLoadDialog();
        }
        xPresent.getV().showMainView();
        Object body = t.getData();
        on_Next((K) body);
        xPresent.getV().twinkCompleted();
    }

    public void on_Next(K k) {
        if (controlDialog) {
            xPresent.getV().disLoadDialog();
        }
    }

}
